﻿using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Data;
namespace WPFExcelReport
{
    public partial class GTTVK05 : DocumentBase
    {
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        public DataTable TBPRG
        {
            get
            {
                return app.TBPRG;
            }
            set
            {
                app.TBPRG = value;
            }
        }
        public DataTable TBUR
        {
            get
            {
                return app.TBUR;
            }
            set
            {
                app.TBUR = value;
            }
        }

        #region ==IsChgZA===========
        public static readonly DependencyProperty IsChgZAProperty = DependencyProperty.Register("IsChgZA",
            typeof(bool), typeof(GTTVK05),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsChgZAChanged));
        public bool IsChgZA
        {
            get
            {
                return (bool)GetValue(IsChgZAProperty);
            }
            set
            {
                SetValue(IsChgZAProperty, value);
            }
        }
        private static void OnIsChgZAChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as GTTVK05;
            if (src != null)
            {
                src.IsChgZA = (bool)e.NewValue;
            }
        }
        #endregion
        #region ==IsPringPR===========
        public static readonly DependencyProperty IsPringPRProperty = DependencyProperty.Register("IsPringPR",
            typeof(bool), typeof(GTTVK05),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsPringPRChanged));
        public bool IsPringPR
        {
            get
            {
                return (bool)GetValue(IsPringPRProperty);
            }
            set
            {
                SetValue(IsPringPRProperty, value);
                OnPrintPRChg();
            }
        }
        private static void OnIsPringPRChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as GTTVK05;
            if (src != null)
            {
                src.IsPringPR = (bool)e.NewValue;
            }
        }
        #endregion
        #region ==IsPringPRCN===========
        public static readonly DependencyProperty IsPringPRCNProperty = DependencyProperty.Register("IsPringPRCN",
            typeof(bool), typeof(GTTVK05),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsPringPRCNChanged));
        public bool IsPringPRCN
        {
            get
            {
                return (bool)GetValue(IsPringPRCNProperty);
            }
            set
            {
                SetValue(IsPringPRCNProperty, value);
                OnPrintPRCNChg();
            }
        }
        private static void OnIsPringPRCNChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as GTTVK05;
            if (src != null)
            {
                src.IsPringPRCN = (bool)e.NewValue;
            }
        }
        #endregion
        #region ==IsPringPREN===========
        public static readonly DependencyProperty IsPringPRENProperty = DependencyProperty.Register("IsPringPREN",
            typeof(bool), typeof(GTTVK05),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsPringPRENChanged));
        public bool IsPringPREN
        {
            get
            {
                return (bool)GetValue(IsPringPRENProperty);
            }
            set
            {
                SetValue(IsPringPRENProperty, value);
                OnPrintPRENChg();
            }
        }
        private static void OnIsPringPRENChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as GTTVK05;
            if (src != null)
            {
                src.IsPringPREN = (bool)e.NewValue;
            }
        }
        #endregion

        VGTTVK05 vm;
        public GTTVK05()
        {
            vm = new VGTTVK05();
            DataContext = vm;
            InitializeComponent();
            this.Name = "销售管理";
            SetUp();
        }
        private void SetUp()
        {
            List<string> deftxt = new List<string>();
            deftxt = new List<string>();
            deftxt.Add("00please type your remark after remove this:");
            cVKK.PRTYPE = "SXNVK04";
            cVKK.IsCheckKD = true;
            cVKK.LFD = "";
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (IsLoaded)
                return;
            SetUpUserRight();
            IsLoaded = true;
        }
        public override void SetUpUserRight()
        {
            base.SetUpUserRight();
            Type mom = this.GetType();
            if (mom == null)
                return;
            string filter = "USERID='" + app.User + "' AND PROG='" + mom.Name.PadRight(80) + "'";
            DataRow[] rows = TBUR.Select(filter);
            foreach (DataRow row in rows)
            {
                string contol = row["CONTROLS"].ToString();//属性 
                string PROCESS = row["PROCESS"].ToString();//控制值
                string txt = row["P0"].ToString();//文本值 
                bool blControl = true;
                if (PROCESS.Trim() == "N")
                {
                    blControl = false; ;
                }
                if (PROCESS.Trim() == "Y")
                {
                    blControl = true; ;
                }
                switch (contol.Trim())
                {
                    case "DEL":
                        IsCanDelete = blControl;
                        break;
                    case "NEW":
                        IsCanNew = blControl;
                        break;
                    case "CHG":
                        IsCanChg = blControl;
                        break;
                    case "COPY":
                        IsCanCopy = blControl;
                        break;
                    case "MS":
                        ScopeManage = txt;
                        break;
                    case "CHGZA":
                        IsChgZA = blControl;
                        break;
                }
            }
        }
        private void OnPrintPRChg()
        {
            try
            {
                IsExpandAll = !IsExpandAll;


                if (!IsPringPR)
                    return;
                IsPringPR = false;
                if (cVKP.dwMaster == null)
                    return;
                DataSet Set = new DataSet();
                cVKP.filter = "VKPART='" + cVKP.TMV.VKPART + "'";
                cVKP.rows = cVKP.TBMain.Select(cVKP.filter);
                cVKP.TBTemp = cVKP.rows.CopyToDataTable();
                cVKP.TBTemp.TableName = "VKPOS";

                Set.Merge(cVKP.TBTemp); 
                Set.Merge(cVKK.TBMain);
                Set.Merge(cKD.TBMain);
                Set.Merge(cZB.TBMain);
                Set.Merge(cZBQTY.GETVKKZBQTY());
                Set.Merge(cWS.TBMain);
                DataRow dwData = cVKP.TMV.VA.TBMain.NewRow();
                dwData["VAAMANDANT"] = app.Mandant;
                dwData["VAAKOLL"] = cVKP.TMV.VKPKOLL;
                dwData["VAAART"] = cVKP.TMV.VKPART;
                Set.Merge(cVKP.TMV.VA.QVA10(dwData));
                dwData = cVKP.TMV.ST.TBMain.NewRow();
                dwData["STUELISAPD"] = cVKP.TMV.VKPART;
                dwData["STUELIMANDANT"] = app.Mandant;
                Set.Merge(cVKP.TMV.ST.QSTU03(dwData));
                dwData = cVKK.TMV.VHR.TBMain.NewRow();
                dwData["HRMANDANT"] = app.Mandant;
                dwData["HRBENID"] = cVKK.TMV.VKKSACHBE;
                Set.Merge(cVKK.TMV.VHR.QHR02(dwData));
                //Set.Merge(app.DBSDDAT["0178"]);
                cDR.PRINT(Set);
                IsExpandAll = !IsExpandAll;
            } 
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace,ex);
            }
         
        }
        private void OnPrintPRCNChg()
        {
            try
            {

                IsExpandAll = !IsExpandAll;
                if (!IsPringPRCN)
                    return;
                IsPringPRCN = false;
                if (cVKP.dwMaster == null)
                    return;
                DataSet Set = new DataSet();
                Set.Merge(cVKK.TBMain);
                Set.Merge(cVKP.TBMain);
                cVKK.PM["KRS"] = cVKK.TMV.VKKAUFKRS;
                cVKK.PM["LFD"] = cVKK.TMV.VKKAUFLFD;
                DataSet SResult= cVKK.TMV.VKKPRNCHK(cVKK.PM);
                Set.Merge(SResult);  
                cDR.PRINT(Set);
                IsExpandAll = !IsExpandAll;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        private void OnPrintPRENChg()
        {
            try
            {
                if (!IsPringPREN)
                    return;
                IsExpandAll = !IsExpandAll;
                IsPringPREN = false;
                if (cVKP.dwMaster == null)
                    return;
                DataSet Set = new DataSet();
                cVKP.filter = "VKPART='" + cVKP.TMV.VKPART + "'";
                cVKP.rows = cVKP.TBMain.Select(cVKP.filter);
                cVKP.TBTemp = cVKP.rows.CopyToDataTable();
                cVKP.TBTemp.TableName = "VKPOS";

                Set.Merge(cVKP.TBTemp); 
                Set.Merge(cVKK.TBMain);
                Set.Merge(cKD.TBMain);
                Set.Merge(cZB.TBMain);
                Set.Merge(cZBQTY.GETVKKZBQTY());
                Set.Merge(cWS.TBMain);
                DataRow dwData = cVKP.TMV.VA.TBMain.NewRow();
                dwData["VAAMANDANT"] = app.Mandant;
                dwData["VAAKOLL"] = cVKP.TMV.VKPKOLL;
                dwData["VAAART"] = cVKP.TMV.VKPART;
                Set.Merge(cVKP.TMV.VA.QVA10(dwData));
                dwData = cVKP.TMV.ST.TBMain.NewRow();
                dwData["STUELISAPD"] = cVKP.TMV.VKPART;
                dwData["STUELIMANDANT"] = app.Mandant;
                Set.Merge(cVKP.TMV.ST.QSTU02(dwData));
                //Set.Merge(app.DBSDDAT["0178"]);
                cDR.PRINT(Set);
                IsExpandAll = !IsExpandAll;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        } 
    }
}
